//name: Xingkai Zhou
//SID:1318620
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

public class Resource {
    public Resource() throws IOException {
        File f=new File(System.getProperty("user.dir"));
        if(!f.exists()){
            f.mkdirs();
        }
        File file = new File(System.getProperty("user.dir") + "/word.json");
        if (!file.exists()) {
            file.createNewFile();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("a", "word a");
            jsonObject.put("b", "word b");
            jsonObject.put("c", "word c");
            storeResource(jsonObject);
        }
    }

    public String queryById(String id) throws Exception {
        synchronized (Resource.class) {
            JSONObject resource = getResource();
            Object result = resource.get(id);
            return result != null ? result.toString() : "Unfortunately, the word was not found in the dictionary!";
        }
    }

    public String deleteById(String id) throws Exception {
        synchronized (Resource.class) {
            JSONObject jsonObject = getResource();
            Object result = jsonObject.get(id);
            if (Objects.nonNull(result)) {
                jsonObject.remove(id);
                storeResource(jsonObject);
                return "Successfully Deleted";
            } else {
                return "Deletion failed because the word does not exist in the dictionary!";
            }
        }

    }

    public String update(String id, String explain) throws Exception {
        synchronized (Resource.class) {
            JSONObject resource = getResource();
            Object result = resource.get(id);
            if (Objects.nonNull(result)) {
                resource.put(id, explain);
                storeResource(resource);
                return "Successfully updated";
            } else {
                return "Update failed because the word is not in the dictionary!";
            }
        }
    }

    public String save(String id, String explain) throws Exception {
        synchronized (Resource.class) {
            JSONObject resource = getResource();
            Object result = resource.get(id);
            if (Objects.isNull(result)) {
                resource.put(id, explain);
                storeResource(resource);
                return "New word added successfully";
            } else {
                return "Adding failed because the word already exists in the dictionary";
            }
        }

    }


    private JSONObject getResource() throws Exception {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(System.getProperty("user.dir") + "/word.json");
            Object obj = new JSONParser().parse(fileReader);
            JSONObject jo = (JSONObject) obj;

            return jo;
        } catch (IOException | ParseException e) {
            throw e;
        } finally {
            if (fileReader != null) {
                fileReader.close();
            }
        }
    }


    private void storeResource(JSONObject jsonObject) throws IOException {
        FileWriter fos = null;
        try {
            fos = new FileWriter(System.getProperty("user.dir") + "/word.json");
            JSONObject.writeJSONString(jsonObject, fos);
        } catch (IOException e) {
            throw e;
        } finally {
            if (fos != null) {
                fos.close();
            }
        }

    }
}
